import re

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pyquery import PyQuery as pq
import os
import urllib.request

# KEYWORD = '长裤'
#browser = webdriver.PhantomJS()
#browser.set_window_size(1400, 900)

browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
print('长裤输入0 七分裤输入1 五分裤输入2 短裤输入3 七分裤／九分裤输入4')
href = input()
if href == "0":
    href = "https://list.tmall.com/search_product.htm?spm=a220m.1000858.1000722.22.4f22379cGoyeau&cat=50025135&q=%BF%E3%D7%D3+%C5%AE&prop=122276111:20525&sort=s&style=g&from=sn_1_prop-qp#J_crumbs"
    KEYWORD = '长裤'
if href == "1":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A30271"
    KEYWORD = '七分裤'
if href == "2":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A30272"
    KEYWORD = '五分裤'
if href == "3":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A20524"
    KEYWORD = '短裤'
if href == "4":
    href = "https://s.taobao.com/search?q=%E8%A3%A4%E5%AD%90+%E5%A5%B3&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&cps=yes&ppath=122276111%3A72202018"
    KEYWORD = '七分裤／九分裤'

def search():

    try:     
        browser.get(href)
        # input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#q')))
        # submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button')))
        # input.send_keys(KEYWORD)
        # submit.click()
        total = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#content > div.main > div.ui-page > div > b.ui-page-skip > form')))
        get_products()
        return total.text
    except TimeoutException:
        return search()

def next_page(page_number):
    print('正在翻页',page_number)
    try:
        input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#content > div.main > div.ui-page > div > b.ui-page-skip > form > input.ui-page-skipTo')))
        submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#content > div.main > div.ui-page > div > b.ui-page-skip > form > button')))
        input.clear()
        input.send_keys(page_number)
        submit.click()
        wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#content > div.main > div.ui-page > div > b.ui-page-num > b.ui-page-cur'), str(page_number)))
        get_products()
    except TimeoutException:
        next_page(page_number)

def get_products():
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#J_ItemList .product-iWrap')))
    html = browser.page_source
    doc = pq(html)
    items = doc('#J_ItemList .product-iWrap').items()
    for item in items:
        try:
            product = {
                
                'commodity_url': item.find('.productImg-wrap .productImg').attr('href'),
                'price': item.find('.productPrice').text(),
                'title': item.find('.productTitle').text(),
                'shop': item.find('.productShop').text(),
                'image_url': re.findall(re.compile('data-ks-lazyload="(.*?)" src="data:', re.S),item.find('.productImg-wrap .productImg').html()),
                 # browser.find_element_by_xpath("./*//a[@class='productImg']/img").get_attribute('src'),
            }
        except Exception as e:
            print("需要登录",e,type(product['image_url']))
            search()
        print(product)
        _download_image(product['image_url'],product['shop'] )

def _download_image(url, folder):
    try:
        if not os.path.isdir(folder):
            os.mkdir(folder)
        for elem in url:
            urll = 'http:'+elem
        print('downloading %s' % urll)
        _filename = lambda s: os.path.join(folder, os.path.split(urll)[1])
        urllib.request.urlretrieve(urll, _filename(urll))
        return _filename(urll)
    except Exception as e:
        print(e)

def main():
    try:
        total = search()
        total = int(re.compile('(\d+)').search(total).group(1))
        for i in range(2, total + 1):
            next_page(i)
    except Exception as e:
        print('出错了',e)
        search()
    finally:
        browser.close()

if __name__ == '__main__':
    main()